package com.gph.saviorframework.wf.controller;

import com.gph.saviorframework.Constants;
import com.gph.saviorframework.wf.service.ActivitiDefService;
import com.gph.saviorframework.wf.service.ActivitiInsService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/" + Constants.FRAMEWORK_REQUEST_PATH + "/workflow")
public class DiagramController {

    @Autowired
    private ActivitiDefService activitiDefService;

    @Autowired
    private ActivitiInsService activitiInsService;

    /**
     * 输出跟踪流程信息
     *
     * @param processInstanceId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/process/trace")
    @ResponseBody
    public List<Map<String, Object>> traceProcess(@RequestParam("pid") String processInstanceId) throws Exception {
        List<Map<String, Object>> activityInfos = activitiInsService.traceProcess(processInstanceId);
        return activityInfos;
    }

    /**
     * 读取资源，通过流程ID
     *
     * @param resourceType      资源类型(xml|image)
     * @param processInstanceId 流程实例ID
     * @param response
     * @throws Exception
     *//*
    @RequestMapping(value = "/resource/process-instance")
    public void loadByProcessInstance(@RequestParam("type") String resourceType, @RequestParam("pid") String processInstanceId, HttpServletResponse response)
            throws Exception {
        InputStream resourceAsStream = null;
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId())
                .singleResult();

        String resourceName = "";
        if (resourceType.equals("image")) {
            resourceName = processDefinition.getDiagramResourceName();
        } else if (resourceType.equals("xml")) {
            resourceName = processDefinition.getResourceName();
        }
        resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
        byte[] b = new byte[1024];
        int len = -1;
        while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
            response.getOutputStream().write(b, 0, len);
        }
    }*/

    /**
     * 读取资源，通过流程定义ID
     *
     * @param resourceType      资源类型(xml|image)
     * @param processDefinitionId 流程实例ID
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/resource/process-definition")
    public void loadByProcessDefinition(@RequestParam("type") String resourceType, @RequestParam("pid") String processDefinitionId, HttpServletResponse response)
            throws Exception {

        try{
            byte[] bytes = activitiDefService.viewProcessDefinitionDiagram(resourceType,processDefinitionId);

            response.getOutputStream().write(bytes);
        }
        catch (Exception ex){
            response.getOutputStream().write(ex.getMessage().getBytes());
        }
    }
}
